<?php

class deposits extends CI_Controller {

    function __construct() {
        parent::__construct();
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
        } else {
            redirect('login/costumer', 'refresh');
        }

        $this->load->model('email');
        $this->load->model('deposit');
        $this->load->model('costumer');
        $this->load->model('rute');
        $this->load->model('airline');
        $this->load->model('ym');
        $this->load->library('upload');
        $this->load->helper('deposit_helper');
        $this->load->helper('user_helper');
    }

    function index() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['i'] = 1;
        $data['id'] = $id;
        $data['yms'] = $this->ym->admin();

        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id);


        $data['rows'] = $this->deposit->index($id);
        $data['content'] = 'deposits/index';
        $this->load->view('template/home1', $data);
    }

    function show() {
        $id = $this->uri->segment(3);
        $data['expire'] = $this->costumer->get_expire($id);
        $data['rows'] = $this->deposit->show($id);
        $data['content'] = 'deposits/show';
        $this->load->view('template/costumer', $data);
    }

    function add() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['yms'] = $this->ym->admin();

        $data['i'] = 1;
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id);
        $data['id'] = $id;

        $this->load->model('admin_bank_model');
        $data['banks'] = $this->admin_bank_model->index();
        $data['content'] = 'deposits/new';
        $this->load->view('template/home1', $data);
    }

    function create() {
        $id_costumer = $this->id();
        $date = date('Y-m-d');
        $jam = $this->input->post('jam', TRUE);
        $menit = $this->input->post('menit', TRUE);
        $jam = $jam . ':' . $menit;

        $data = array(
            'costumer_id' => $id_costumer,
            'cara_pembayaran' => $this->input->post('cara_pembayaran', TRUE),
            'tgl_transfer' => $this->input->post('tgl_transfer', TRUE),
            'jam' => $jam,
            'no_rekening' => $this->input->post('no_rekening', TRUE),
            'validation_code' => $this->input->post('validation_code', TRUE),
            'berita_transfer' => $this->input->post('berita_tansfer', TRUE),
            'bank_name' => $this->input->post('bank_name', TRUE),
            'costumer_bank' => $this->input->post('costumer_bank', TRUE),
            'costumer_an' => $this->input->post('costumer_an', TRUE),
            'deposit' => $this->input->post('deposit', TRUE),
            'date' => $date,
        );

        $data['costumer_an'] = $this->input->post('costumer_an', TRUE);
        $data['deposit'] = $this->input->post('deposit', TRUE);
        $data['date'] = $date;

        $email = $this->email->email_array();
//        $email_user = $this->costumer->get_email($id_costumer);
//        $email_user = array($email_user);
        $message = $this->load->view('costumers/email_deposit', $data, TRUE);
        $this->load->library('email');
        $this->email->set_mailtype("html");
        $this->email->from('stasiuntiketonline.com');

        //email

//        $list = array_merge($email, $email_user);
        $this->email->to($email);
        $this->email->cc('');
        $this->email->subject('Deposit');
        $this->email->message($message);
        $this->email->send();
        
        $this->deposit->create($data);
    }

    private function id() {
        $session_data = $this->session->userdata('logged_in');
        return $session_data['id'];
    }

    function history_fee() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['i'] = 1;
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id);

        $data['rows'] = $this->deposit->fee($id);
        $data['content'] = 'deposits/fee';
        $this->load->view('template/home1', $data);
    }

    function refund() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['i'] = 1;
        $data['yms'] = $this->ym->admin();
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id);

        $data['content'] = 'deposits/refund';
        $this->load->view('template/home1', $data);
    }

    function refund_create() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000000';
        $config['max_width'] = '5000';
        $config['max_height'] = '5000';
        $this->upload->initialize($config);

        $pic = '';

        if (isset($_FILES["userfile"]["name"])) {
            if (!$this->upload->do_upload()) {
                redirect('deposits/refund/gagal');
            } else {
                //array_push($data,array('error' => 'success'));

                $date = date('y-m-d');
                $tGambar = $this->upload->data();
                $pic = $tGambar["file_name"];
                $ktp = $_FILES["userfile"]["name"];

                $data = array(
                    'id_user' => $id,
                    'id_maskapai' => $this->input->post('id_maskapai', TRUE),
                    'code_booking' => $this->input->post('code_booking', TRUE),
                    'no_tiket' => $this->input->post('no_tiket', TRUE),
                    'from' => $this->input->post('from', TRUE),
                    'to' => $this->input->post('to', TRUE),
                    'nama' => $this->input->post('nama', TRUE),
                    'keterangan' => $this->input->post('keterangan', TRUE),
                    'ktp' => $ktp,
                    'date' => date('y-m-d')
                );
                $this->deposit->refund_create($data);
            }
        } else {
            $pic = '';
        }
    }

    function history_refund() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['yms'] = $this->ym->admin();

        $data['i'] = 1;
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id);

        $data['rows'] = $this->deposit->hrefund($id);
        $data['content'] = 'deposits/hrefund';
        $this->load->view('template/home1', $data);
    }

    function showrefund() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['yms'] = $this->ym->admin();

        $data['i'] = 1;
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id);

        $id_refund = $this->uri->segment(3);
        $data['rows'] = $this->deposit->showrefund($id_refund, $id);
        $data['content'] = 'deposits/showrefund';
        $this->load->view('template/home1', $data);
    }

}

?>
